package com.doctcloud.hisinfo.medrec.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.doctcloud.hisinfo.histable.domain.medrec.PatMasterIndexDo;
import com.doctcloud.hisinfo.medrec.mapper.PatMasterIndexDoMapper;
import com.doctcloud.hisinfo.medrec.service.IPatMasterIndexService;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

@Service
public class PatMasterIndexService extends ServiceImpl<PatMasterIndexDoMapper, PatMasterIndexDo> implements IPatMasterIndexService {
    @Override
    public PatMasterIndexDo getById(String patientId) {
        PatMasterIndexDo resdata = new PatMasterIndexDo();
        LambdaQueryWrapper<PatMasterIndexDo> query = Wrappers.lambdaQuery();
        query.eq(PatMasterIndexDo::getPatientId,patientId);
        List<PatMasterIndexDo> patLists = baseMapper.selectList(query);
        if (CollectionUtils.isNotEmpty(patLists)){
            resdata = patLists.get(0);
        }
        return resdata;
    }

    @Override
    public List<PatMasterIndexDo> getByIds(List<String> patientIds) {
        if (CollectionUtils.isEmpty(patientIds)) {
            return Collections.emptyList();
        }
        // 2. 创建 LambdaQueryWrapper
        LambdaQueryWrapper<PatMasterIndexDo> queryWrapper = Wrappers.lambdaQuery();

        // 3. 使用 .in() 方法添加条件，匹配 patientIds 列表中的任意一个ID
        queryWrapper.in(PatMasterIndexDo::getPatientId, patientIds);

        // 4. 执行查询
        List<PatMasterIndexDo> patMasterIndexDos = baseMapper.selectList(queryWrapper);

        // 5. 如果查询结果为空，返回一个不可变的空列表，而不是 null，增强代码健壮性
        return CollectionUtils.isEmpty(patMasterIndexDos) ? Collections.emptyList() : patMasterIndexDos;
    }
}
